package com.driving.single.frontend.coach.service.impl;

import com.driving.single.common.coach.service.CommonCoachClassesService;
import com.driving.single.common.coach.service.CommonCoachCommentService;
import com.driving.single.common.coach.service.CommonCoachService;
import com.driving.single.common.domain.TbCoach;
import com.driving.single.common.domain.TbCoachClasses;
import com.driving.single.common.entity.DrivingResult;
import com.driving.single.common.entity.PageResult;
import com.driving.single.common.redis.service.CommonRedisCoachClassesService;
import com.driving.single.common.redis.service.CommonRedisCoachService;
import com.driving.single.frontend.coach.service.FrontendCoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FrontendCoachServiceImpl implements FrontendCoachService {
    @Autowired
    private CommonRedisCoachService commonRedisCoachService;
    @Autowired
    private CommonRedisCoachClassesService commonRedisCoachClassesService;
    @Autowired
    private CommonCoachService commonCoachService;
    @Autowired
    private CommonCoachCommentService commonCoachCommentService;
    @Autowired
    private CommonCoachClassesService commonCoachClassesService;

    /**
     * 实现根据主键查询教练接口
     *
     * @param id 主键
     */
    @Override
    public DrivingResult selectCoachById(Long id) {
        // 1.查询Redis缓存
        try {
            TbCoach redisTbSchool = this.commonRedisCoachService.selectRedisTbCoachById(id);
            if (redisTbSchool != null) {
                return DrivingResult.ok(redisTbSchool);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 2.查询数据库
        TbCoach tbCoach = this.commonCoachService.selectTbCoachById(id);
        if (tbCoach == null) {
            return DrivingResult.error("查无数据");
        }
        // 3.插入Redis缓存
        try {
            this.commonRedisCoachService.insertRedisTbCoach(tbCoach);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.ok(tbCoach);
    }

    /**
     * 实现找教练页面驾校查询 接口
     *
     * @param page        当前页
     * @param rows        页显示数
     * @param area        查询条件：区域(学校名称)
     * @param licenseType 查询条件：驾照类型
     * @param minPrice    查询条件：最小价格
     * @param maxPrice    查询条件：最大价格
     */
    @Override
    public DrivingResult selectTbCoachListByConditionWithPage(Integer page, Integer rows, String area,
                                                              String licenseType, Integer minPrice, Integer maxPrice) {
        // 1.价格输入校验
        if (maxPrice != null && minPrice != null && maxPrice < minPrice) {
            return DrivingResult.error("价格区间有误，请核实输入！");
        }
        // 2.分页查询数据库
        PageResult pageResult = this.commonCoachService.selectTbCoachListByConditionWithPage(page, rows, area,
                licenseType, minPrice, maxPrice);
        if (pageResult == null || pageResult.getResult().size() == 0) {
            return DrivingResult.error("查无数据");
        }
        return DrivingResult.ok(pageResult);
    }

    /**
     * 实现根据教练ID   分页查询教练评论   接口
     *
     * @param coach_id 教练主键
     * @param page     当前页
     * @param rows     页显示数
     */
    @Override
    public DrivingResult selectCoachCommentByCoachIdWithPage(Long coach_id, Integer page, Integer rows) {
        PageResult pageResult = this.commonCoachCommentService.selectCoachCommentByCoachIdWithPage(coach_id,
                page, rows);
        if (pageResult == null || pageResult.getResult().size() == 0) {
            return DrivingResult.error("查无数据");
        }
        return DrivingResult.ok(pageResult);
    }

    /**
     * 实现根据教练ID   查询教练班级及费用   接口
     *
     * @param coach_id 教练主键
     */
    @Override
    public DrivingResult selectCoachClassesByCoachId(Long coach_id) {
        // 1.查询Redis缓存
        try {
            List<Map<String, Object>> redisList =
                    this.commonRedisCoachClassesService.selectRedisTbCoachClassesByCoachId(coach_id);
            if (redisList != null && redisList.size() > 0) {
                return DrivingResult.ok(redisList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 2.查询数据库
        List<TbCoachClasses> tbCoachClassesList = this.commonCoachClassesService.selectCoachClassesByCoachId(coach_id);
        if (tbCoachClassesList == null || tbCoachClassesList.size() == 0) {
            return DrivingResult.error("查无数据");
        }
        // 3.数据加工
        Map<String, Object> map = null;
        List<Map<String, Object>> mapTbCoachClassesList = new ArrayList<>();
        for (TbCoachClasses tbCoachClasses : tbCoachClassesList) {
            map = new HashMap<>();
            try {
                map.put("isPick", tbCoachClasses.getIsPick());
                map.put("alonePrice", tbCoachClasses.getAlonePrice());
                map.put("teamPrice", tbCoachClasses.getTeamPrice());
                map.put("licenseType", Arrays.asList(tbCoachClasses.getLicenseType().split(",")));
                map.put("teachTime", Arrays.asList(tbCoachClasses.getTeachTime().split(",")));
            } catch (Exception e) {
                e.printStackTrace();
            }
            mapTbCoachClassesList.add(map);
        }
        // 4.插入Redis缓存
        try {
            this.commonRedisCoachClassesService.insertRedisCoachClasses(mapTbCoachClassesList, coach_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.ok(mapTbCoachClassesList);
    }
}
